<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------
namespace app\api\home;

use think\Controller;
use think\Db;
use app\common\model\PhoneCode;
use think\Request;
use app\common\model\RedLog;

class Redpacket extends Controller
{
    protected $requestData;

    public function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Headers:content-type');
        header('Access-Control-Request-Method:GET,POST');
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
            exit;
        }
        $request = Request::instance();
        $this->requestData = $request->param();
    }

    /**
     * 获取红包状态
     * @author
     */
    public function getPacket()
    {
        $postData = $this->requestData;
        $redCode = $postData['redCode'];
        $map['code'] = $redCode;
        $map['is_active'] = 1;
        $map['status'] = 1;
        $redInfo = Db::name('redpacket')->field('id,num,getnum,content')->where($map)->find();
        if ($redInfo) {
            $redLogModel = new RedLog();
            $LogList = $redLogModel->getList($redInfo['id'],0,20);
            $redInfo['list'] = $LogList;
            $result = [
                'status' => 1,
                'msg' => $redInfo
            ];
        } else {
            $result = [
                'status' => 0,
                'msg' => 'no redpacket'
            ];
        }
        echo json_encode($result);
        exit;

    }

    /**
     * 发送短信
     * @author
     */
    public function sendMsg()
    {
        $postData = $this->requestData;
        if (!$postData['mobile']) {
            $data['status'] = 0;
            $data['code'] = 1;
            $data['data'] = '参数不全';
            echo json_encode($data);
            exit();
        }
        $codeModel = new PhoneCode();
        $res = $codeModel->send($postData['mobile'], 0, 4);
        if ($res) {
            $res = array(
                'status' => 1,
                'content' => '发送成功',
                'code' => 0,
            );
        } else {
            $res = array(
                'status' => 0,
                'content' => '服务繁忙',
                'code' => 1,
            );
        }
        echo json_encode($res);
        exit();
    }

    /**
     * 抢红包操作
     * @author
     */
    public function optionRedPacket()
    {
        $postData = $this->requestData;
        if (!$postData['mobile'] || !$postData['telcode']) {
            $data['status'] = 0;
            $data['code'] = 1;
            $data['data'] = '参数不全';
            echo json_encode($data);
            exit();
        }
        $phoneCode = new PhoneCode();
        $verifiPhone = $phoneCode->verifiregCode($postData['mobile'], 4, $postData['telcode']);
        if (!$verifiPhone) {
            $result = array(
                'status' => 0,
                'data' => 'phoneCode wrong',
                'code' => 2,
            );
            echo json_encode($result);
            exit;
        }
        $redCode = $postData['redCode'];
        $map['code'] = $redCode;
        $map['is_active'] = 1;
        $map['status'] = 1;
        $redInfo = Db::name('redpacket')->where($map)->find();
        if ($redInfo) {
            $checkMap['red_id'] = $redInfo['id'];
            $checkMap['mobile'] = $postData['mobile'];
            $isGet = Db::name('red_log')->where($checkMap)->field('id')->find();
            if($isGet){
                $result = array(
                    'status' => 0,
                    'data' => 'already get',
                    'code' => 5,
                );
                echo json_encode($result);
                exit;
            }
            $overMany = $redInfo['money'] - $redInfo['getmoney'];
            $overNum = $redInfo['num'] - $redInfo['getnum'];
            if ($overNum > 1) {
                $maxMony = floor($overMany/$overNum*2*100);
                $getMony = rand(1,$maxMony) / 100;
            } elseif ($overNum == 1) {
                $getMony = $overMany;
            } else {
                $result = array(
                    'status' => 0,
                    'data' => 'redpacket none',
                    'code' => 3,
                );
                echo json_encode($result);
                exit;
            }
            $umap['mobile'] = $postData['mobile'];
            $umap['status'] = 1;
            $uinfo = Db::name('users')->where($umap)->field('id')->find();
            Db::startTrans();
            try {
                if ($uinfo) {
                    $ucmap['user_id'] = $uinfo['id'];
                    $ucmap['currency_id'] = 2;
                    $ucmap['status'] = 1;
                    $ucRes = Db::name('users_currency')->where($ucmap)->setInc('num', $getMony);
                    if ($ucRes) {
                        $isGive = 1;
                    } else {
                        $isGive = 0;
                    }
                } else {
                    $isGive = 0;
                }
                $insertData = [];
                $insertData['red_id'] = $redInfo['id'];
                $insertData['mobile'] = $postData['mobile'];
                $insertData['money'] = $getMony;
                $insertData['ctime'] = time();
                $insertData['is_give'] = $isGive;
                Db::name('red_log')->insert($insertData);
                Db::name('redpacket')->where($map)->setInc('getnum');
                Db::name('redpacket')->where($map)->setInc('getmoney',$getMony);
                Db::commit();
                $result = array(
                    'status' => 1,
                    'data' => $insertData,
                    'code' => 0,
                );
                echo json_encode($result);
                exit;
            }catch (\Exception $e) {
                Db::rollback();
                $result = array(
                    'status' => 0,
                    'data' => 'option failed',
                    'code' => 4,
                );
                echo json_encode($result);
                exit;
            }
        } else {
            $result = array(
                'status' => 0,
                'data' => 'redpacket none',
                'code' => 3,
            );
            echo json_encode($result);
            exit;
        }
    }


}